<?php

	defined( 'APP_PATH' )
		|| define( 'APP_PATH', levelUp( dirname( __FILE__ ) ) );
	defined( 'INCLUDE_PATH' )
		|| define( 'INCLUDE_PATH', APP_PATH . 'components' . DIRECTORY_SEPARATOR );
	defined( 'PHP_PATH' )
		|| define( 'PHP_PATH', APP_PATH . 'php' . DIRECTORY_SEPARATOR );
	defined( 'TPL_PATH' )
		|| define( 'TPL_PATH', APP_PATH . 'tpl' . DIRECTORY_SEPARATOR );
	defined( 'PUBLIC_PATH' )
		|| define( 'PUBLIC_PATH', APP_PATH . 'public' . DIRECTORY_SEPARATOR );	
	defined( 'ADMIN_URL_PART' )
		|| define( 'ADMIN_URL_PART', 'admin' );
	defined( 'DEFAULT_ADMIN_PAGE' )
		|| define( 'home', 'home' );

	require_once( INCLUDE_PATH . 'functions.php' );
	require_once( INCLUDE_PATH . 'Loader.php' );

	spl_autoload_register( null, false );
	spl_autoload_extensions( '.php' );
	spl_autoload_register( Array( 'Loader', 'includeClass' ) );
	register_shutdown_function( 'application_shutdown_handler' );
	ini_set( 'display_errors', $applicationLevelDebug ? 1 : 0 );

	function levelUp( $dir ) {
		$dir = trim( $dir, DIRECTORY_SEPARATOR );
		$path = explode( DIRECTORY_SEPARATOR, $dir );
		array_pop( $path );
		return ( DIRECTORY_SEPARATOR == '/' ? '/' : '' ) . implode( DIRECTORY_SEPARATOR, $path ) . DIRECTORY_SEPARATOR;
	}
	
	function wpLog( $thing ) {
		ob_start();
		print_r( $thing );
		$data = trim( ob_get_contents() );
		ob_end_clean();
		//ob_end_flush();
		if( $data ) {
			file_put_contents( APP_PATH . 'pramodthakur.log', "\n$data\n", FILE_APPEND );
		}
	};

	function doInternaServerError( $html = false ) {
		$html = $html ? $html : '<div style="font-size:18px;line-height:1.6em;margin: 50px auto 40px auto;width: 600px;text-align: center;"><p><strong>Looks like something went wrong!</strong></p><p>We track these errors automatically, but if the problem persists feel free to contact us. In the meantime, try refreshing.</p></div>';
		header( 'HTTP/1.0 500 Internal Server Error', true );
		echo '<!DOCTYPE html><html><head><meta charset="UTF-8" /><title>500 Internal Server Error</title></head><body>' . $html . '</body></htmL>';
	}

	function application_shutdown_handler() {
		global $applicationLevelDebug;
		if( !is_null( $lastError = error_get_last() ) && !$applicationLevelDebug ) {
			if( array_search( $lastError[ 'type' ], array( E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR ) ) !== false ) {
				doInternaServerError();
				
				wpLog( 'Error on pramodthakur Site at ' . date( 'Y-m-d H:i:s' ) . "\n" . $_SERVER[ 'HTTP_HOST' ] . "\n" . $_SERVER[ 'REQUEST_URI' ] . "\n" . $lastError[ 'message' ] . ' in file ' . $lastError[ 'file' ] . ' on line ' . $lastError[ 'line' ] . '.' );
				
				$mail = new Mail();
				$mail->setFrom( 'server-error@' . ( defined( HOST_NAME ) ? HOST_NAME : 'pramodthakur.com' ), "Pramod Thakur" )->addRecipient( 'info@pramodthakur.com', 'Pramod Thakur' );
				$mail->buildMail( $_SERVER[ 'HTTP_HOST' ] . ' : Error on pramodthakur Site at ' . date( 'Y-m-d H:i:s' ), $_SERVER[ 'REQUEST_URI' ] . "\n" . $lastError[ 'message' ] . ' in file ' . $lastError[ 'file' ] . ' on line ' . $lastError[ 'line' ] . '.', '<pre style="font-size:15px;line-height:1.6em">' . $_SERVER[ 'REQUEST_URI' ] . "\n" . $lastError[ 'message' ] . ' in file ' . $lastError[ 'file' ] . ' on line ' . $lastError[ 'line' ] . '.' . '</pre>' );
				$mail->sendMail();
			}
		}
	}

	try {
		session_start();
		Url::processUrl();
		$requireFile = Url::getUrlFileUri();
		$arrDomainUri = Url::getSplittedDomainURI();
		$hostName = Url::getHostName();
		$adminPage = Url::getAdminPage();
		
		$subDomain = Url::getsubDomainValue();
		
		defined( 'HOST_NAME' )
			|| define( 'HOST_NAME', $hostName );
		defined( 'HOME_PATH' )
			|| define( 'HOME_PATH', "http://$subDomain.$hostName/" );

		$fileName = explode( DIRECTORY_SEPARATOR, $requireFile );
		$tplFile = str_replace( DIRECTORY_SEPARATOR . 'php' . DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR . 'tpl' . DIRECTORY_SEPARATOR, str_replace( '.php', '.tpl.php', $requireFile ) );
		if( file_exists( $tplFile ) ) {
			$template = new PHPTAL( $tplFile );
			require_once( $requireFile );
			$template->absolutePath = HOME_PATH;
			echo $template->execute();
		} else {
			require_once( $requireFile );
		}
	} catch( Exception $ex ) {
		if( $applicationLevelDebug ) {
			describe( $ex->getMessage() . "\n" . $ex->getTraceAsString() );
		} else {
			doInternaServerError();
			
			wpLog( 'Error on pramodthakur Site at ' . date( 'Y-m-d H:i:s' ) . "\n" . $_SERVER[ 'HTTP_HOST' ] . "\n" . $_SERVER[ 'REQUEST_URI' ] . "\n" . $ex->getMessage() . "\n" . $ex->getTraceAsString() );
			
			$mail = new Mail();
			$mail->setFrom( 'server-error@' . ( defined( HOST_NAME ) ? HOST_NAME : 'pramodthakur.com' ), "Pramod Thakur" )->addRecipient( 'info@pramodthakur.com', 'Pramod Thakur' );
			$mail->buildMail( $_SERVER[ 'HTTP_HOST' ] . ' : Error on pramodthakur Site at ' . date( 'Y-m-d H:i:s' ), $_SERVER[ 'REQUEST_URI' ] . "\n" . $ex->getMessage() . "\n" . $ex->getTraceAsString(), '<pre style="font-size:15px;line-height:1.6em">' . $_SERVER[ 'REQUEST_URI' ] . "\n" . $ex->getMessage() . "\n" . $ex->getTraceAsString() . '</pre>' );
			$mail->sendMail();
		}
	}